<?php
/**
 * This is a tool interface, to help the user maintain
 * the company database clean.
 * It allows to merge company that have been created has duplicates
 * 
 * 
 */

use Fox\Dao\SqlFragment;
use Hive\Company;
use Fox\Html\Screen;
use Fox\Behaviors;
use Fox\Generator;
use Fox\Html\Input;
use Fox\Html\Button;
$xview =  Xview::instance();
/* @var $contact Hive\Company */
$company = $xview->component('company', 'object');
$cpid = $company->getId();

// Customizing
$customizing = Hive\Company\Customizing::instance();
$customizing->trackSummaryPage(array('mod' => 'company','act'=>'tools','sac'=>'merge'));

$cpid = $company->getId();
$header = new Fox\Html\Header\Content('merge',Msg(Hive::BUNDLE,Hive::MENU_MERGE));

$searchScreen = new \Fox\Html\Screen('eBasicSearch','vlook',null,true,null,1,false,true,'#searchResult');
$searchScreen->message('<p class="prettyprint">'.Msg(Hive::BUNDLE,Hive::COMPANY_MERGE_HELP,$company->name()).'</p>')
			->action(Generator::url('company','tools','merge',array('cpid' => $cpid)));	
$searchScreen
		->block()
		->add(new Input('company'))->parentObject()
		->add(new Input('dbcnt',true,100));			
$grid = Xview::instance()->component(Company::BUNDLE,'result');
		/* @var $grid \Fox\Grid */
		$grid->setCoche();
		$grid->fieldcat()
				->setGenerators(array(
					'coche' => Generator::useValue(false)
				));
				
if($searchScreen->submitted()) {
	$fields = $searchScreen->get();
	if(!empty($fields['company'])) {
		$cleanWords = Company::cleanup($fields['company']);
		$cleanWords = '%'.str_replace(' ','%',$cleanWords).'%';
		$fragments[] = new SqlFragment('company_keyname LIKE ?',array($cleanWords));
	}
	if(!$searchScreen->errors()) {
		$query = Fox\Dao::infos(Company::className())
					->handle()->createQueryBuilder();
		$query->select('compid','company_name','uname','udate')
				->from('companies','cp')
				->setMaxResults($fields['dbcnt']);
				
		$fragments[] = new SqlFragment('loekz = ? AND compid != ?',array(0,$cpid));
		$fragment = SqlFragment::combine($fragments);
		$data = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
		
		$grid->layout()->scrollVertical(true);
		$grid->fieldcat()->behaviors(\Fox\Behaviors::key(\Fox\Generator::useElement('compid')));
		$grid->footer()->add(new Button('merge',null,Msg(Hive::BUNDLE,Hive::MENU_MERGE)))
				->behaviors(Behaviors::action(
					Generator::url('company','merge','execute',array('cpid'=> $cpid)),
					false,
					null,
					null,
					false,
					'POST',
					'searchResult',
					null,
					null,
					true
					
				));
		$grid->footer()->add(new Button('merge-delete',null,Msg(Hive::BUNDLE,Hive::MERGE_AND_DELETE)))
				->color(Button::COLOR_RED)
				->behaviors(Behaviors::action(
					Generator::url('company','merge','execute',array('cpid'=> $cpid,'mode' => 'delete')),
					false,
					null,
					null,
					false,
					'POST',
					'searchResult',
					'tr',
					null,
					true
					
				));
		$grid->initialize($data);
		 
	}
	return $grid->display();
}
$html = $searchScreen->render().'<div id="searchResult"></div>';
?>
<div id="messagesContent">
	<div><?php echo $header->render();?></div>
	<div class="contentArea">
		<div class="mbs gsl" id="contacts-container">
				<?php echo $html; ?>
		</div>
	</div>
</div>